var combo_select_user = new Ext.form.ComboBox({
			xtype : 'combo',
			fieldLabel : '<span style="color: red">*&nbsp;</span>用户',
			hiddenName : 'userId',
			valueField : 'id',
			displayField : 'userName',
			mode : 'remote',
			store : ds_user_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			width: 200,
			typeAhead: true,
			readOnly: true,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择用户',
			listeners : {
				'select' : function(combo, record, index) {
					grid_userrole.getSelectionModel().clearSelections();
					ds_userrole.baseParams.userId = combo.getValue();
					ds_userrole.load();
//					grid_userrole.render();
				}
			}});

var btn_save_userrole = new Ext.Button({
	text : '保存',
	iconCls : 'icon-edit',
	handler : function() {
		var records = grid_userrole.getSelectionModel().getSelections();
		var isNotRecords = (records == null || records.length  == 0);
		  var ids = [];
            for(var i=0; i<records.length; i++){
                var pId = records[i].get('id');  
                ids.push(pId);                //向数组后添加元素
        }
		Ext.Msg.confirm('确认修改', isNotRecords ? '确认取消所有的权限?' : '确认要修改用户的权限?', function(btn) {
			if (btn == 'yes') {
				Ext.Ajax.request({
							url : '/supervise/sys/userrole/saveUserRole.action',
							params : {
								ids : ids,
								userId: combo_select_user.getValue()
							},
							success : function() {
								Ext.Msg.show({
											title : '提示',
											msg : '成功修改用户权限!',
											buttons : Ext.Msg.OK,
											icon : Ext.Msg.OK
										});
							},
							failure : function() {
								Ext.Msg.show({
											title : '错误提示',
											msg : '修改时发生错误!',
											buttons : Ext.Msg.OK,
											icon : Ext.Msg.ERROR
										});
							}
						});
			}
		});
	}
});
var cm_userrole = new Ext.grid.ColumnModel([
	new Ext.grid.CheckboxSelectionModel({singleSelect : false}),
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
    {id: 'name',header: "角色名称", width: 200, sortable: true, dataIndex: 'name'},
    {id: 'memo',header: "备注", width: 300,dataIndex: 'memo'},
    {
			id : 'checked',
			header : "是否有权限",
			width : 10,
			hidden : true,
			dataIndex : 'checked',
			renderer : function(value, meta, rcd, rIndex, cIndex, st) {
				if (value) {
					var model = grid_userrole.getSelectionModel();
					model.selectRow(rIndex, true);
				}
			}
		}]);
  
var ds_userrole = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/sys/userrole/findAllUserRole.action'
	}),
	reader : new Ext.data.JsonReader({
		root : 'root',
		successProperty :'success'
	}, [
	{name : 'id',type : 'int'}, 
	{name : 'name',type : 'string'}, 
	{name : 'memo',type : 'string'},
	{name : 'checked',type : 'boolean'}
	])
});
 
var grid_userrole = new Ext.grid.GridPanel({
	title : '用户角色管理',
	iconCls : 'icon-grid',
	loadMask : {
		msg : '数据加载中...'
	},
	region : 'center',
	cm : cm_userrole,
	ds : ds_userrole,
	deferowrender: false,
	sm : new Ext.grid.CheckboxSelectionModel({
		singleSelect : false
	}),
	enableColumnMove : false,
	trackMouseOver : false,
	stripeRows : true,
	frame : true,
	clicksToEdit : 1,
	tbar : ['<span style="color: red">*&nbsp;</span>用户 ',combo_select_user, '-', '&nbsp;&nbsp;&nbsp;',
		btn_save_userrole],
	listeners : {
				'render' : function(e) {
					ds_permission.load();
				}
			}
});

var p_userrole = {
	id : 'userrole-panel',
	border : false,
	layout : 'border',
	items : [grid_userrole]
};